﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-

'slot usage'

__author__='zhaoxp'

from types import MethodType

class Student(object):
	__slots__=('name','age','score','set_age','set_score')
	pass
	
def set_age(self,age):
	self.age=age

def set_score(self,score):
	self.score=score
	
if __name__=='__main__':
	print "slot uage"
	print "dir(Student) =",dir(Student)
	s=Student()
	print "s=Student() =",s
	print "dir(s) =",dir(s)
	s.name='Jack'
	print "dir(s) =",dir(s)
	print "s.name =",s.name
	s.set_age=MethodType(set_age,s,Student)# 给实例绑定一个方法
	s.set_age(23)
	print "s.age =",s.age
	print "dir(s) =",dir(s)
	s2=Student()
	print "dir(s2) =",dir(s2)
	print "hasattr(s2,'name') =",hasattr(s2,'name')
	print "hasattr(s2,'age') =",hasattr(s2,'age')
	print "hasattr(s2,'set_age') =",hasattr(s2,'set_age')
	Student.set_score=MethodType(set_score,None,Student)
	s.set_score(80)
	s2.set_score(90)
	print "hasattr(s2,'score') =",hasattr(s2,'score')
	print "hasattr(s2,'set_score') =",hasattr(s2,'set_score')
	print "s.score =",s.score
	print "s2.score =",s2.score
	
	print "---------- last test ----------"
	print "it will throw error to run following "
	print s2.sex='male'
	
	